package com.company.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.support.SessionStatus;


import com.company.form.AccountForm;
import com.company.validator.AccountValidator;



@Controller
@RequestMapping("/Account")
public class AccountController {

    AccountValidator accountvalidator;
	
	@Autowired
	public AccountController(AccountValidator accountValidator){
		this.accountvalidator = accountValidator;
	}
	
	//cuando llego a la principal de administracion
	@RequestMapping(method = RequestMethod.GET)
	public String initForm(ModelMap model){
		return "CustomerForm";
	}
	
	
	//cuando inicializo para ir a la pantalla de nuevo
	@RequestMapping(value="/new",method = RequestMethod.GET)
	public String initFormNew(ModelMap model){
		return "AccountFormNewView";
	}
	//cuando realizo submit para la pantalla nuevo
	@RequestMapping(value="/new",method = RequestMethod.POST)
	public String AccountNewprocessSubmit(
			@ModelAttribute("AccountForm") AccountForm MiAccount,
			BindingResult result, SessionStatus status) {
		
		accountvalidator.validate(MiAccount, result);
		
		if (result.hasErrors()) {
			//if validator failed
			return "AccountFormNewView";
		} else {
			status.setComplete();
			//form success
			return "AccountFormExito";
		}
	}
	
	
}
